
package com.vinaprime.primeschedule.businesslayer;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.List;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.Cursor;

import com.vinaprime.primeschedule.databaselayer.VPGroupHeader_DL;
import com.vinaprime.primeschedule.entities.VPContact;
import com.vinaprime.primeschedule.entities.VPGroupHeader;
import com.vinaprime.primeschedule.entities.VPGroupLine;
/** * 
 * @author LONGPN
 * @since 17/06/2013
 *
 */
@SuppressLint("DefaultLocale")
public class VPGroupHeader_BL {
	private VPGroupHeader_DL myGroupHeaderDL ;
	private VPGroupLine_BL myGroupLineBL ;
	
	public VPGroupHeader_BL (Context context , String path){
		this.myGroupHeaderDL = new VPGroupHeader_DL(context, path);
		this.myGroupLineBL = new VPGroupLine_BL(context, path);
		
	}

	public int getNewGroupHeaderID (){
		
		int result = 0;
		Cursor cur =  myGroupHeaderDL.getNewGroupHeaderID();
		if (cur.moveToFirst())
		{
			if (!cur.isNull(cur.getColumnIndexOrThrow(VPGroupHeader_DL.getDbVpghId()))) {
				result = Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPGroupHeader_DL.getDbVpghId())));
			}
		}
		return result+1;
	}

	public VPGroupHeader getGroupHeaderbyID (int Id) {
		VPGroupHeader myGH = new VPGroupHeader();
		Cursor cur  =  this.myGroupHeaderDL.getGroupHeaderbyId(Id);
		if(cur.moveToFirst()){
					
				myGH.setVpghId(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPGroupHeader_DL.getDbVpghId()))));
				myGH.setVpghCode(cur.getString(cur.getColumnIndexOrThrow(VPGroupHeader_DL.getDbVpghCode())));
				myGH.setVpghName(cur.getString(cur.getColumnIndexOrThrow(VPGroupHeader_DL.getDbVpghName())));
				myGH.setVpghDescription(cur.getString(cur.getColumnIndexOrThrow(VPGroupHeader_DL.getDbVpghDescription())));
				myGH.setVpghEnableFlag(cur.getString(cur.getColumnIndexOrThrow(VPGroupHeader_DL.getDbVpghEnableFlag())));
				///lay list contact
				List<VPGroupLine> _listLine = myGroupLineBL.getGroupLineList(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPGroupHeader_DL.getDbVpghId()))));
				myGH.setListContactLine(_listLine);
		}
		return myGH;		
	}
	public void updateContactInGroup(int groupHeaderID,List<VPContact> listContactOfGroup)
	{
		List<VPGroupLine> _listLine = myGroupLineBL.getGroupLineList(groupHeaderID);
		if (listContactOfGroup.isEmpty())
		{
			for(VPGroupLine _line : _listLine)
			{
				myGroupLineBL.deleteGroupLine(_line);
			}
		}
		else
		{
				
				
				//Contact is deleted !!
				for(VPGroupLine _line : _listLine)
				{
					int del = 1;
					for (VPContact _contact : listContactOfGroup)
					{
						if(_line.getmContact().getVpcID() == _contact.getVpcID())
						{
							del = 0;
							break;
						}
					}
					if(del == 1)
					{
						myGroupLineBL.deleteGroupLine(_line);
						_listLine.remove(_line);
					}
				}
				
				//Contact is added
				for (VPContact _contact : listContactOfGroup)
				{
					int add = 1;
					for (VPGroupLine _line : _listLine)
					{
						if(_line.getmContact().getVpcID() == _contact.getVpcID())
						{
							add = 0;
							break;
						}
					}
					if(add == 1)
					{
						
						VPGroupLine newLine = new VPGroupLine(
								myGroupLineBL.getNewGroupLineID(), 
								groupHeaderID, 
								_contact, 
								"", 
								"Y");
						
						myGroupLineBL.addGroupLine(newLine);
						//_listLine.add(newLine);
					}
				}
		};
	}
	public List<VPContact> getAllContactFromGroup(int _groupID) throws IllegalArgumentException, ParseException
	{
		List<VPContact> listContact = new ArrayList<VPContact>();
		
		listContact   = this.myGroupLineBL.getAllContactFromGroup(_groupID);
		return listContact;
	}
	public List<VPGroupHeader> getAllGroupHeader() throws IllegalArgumentException, ParseException
	{
		List<VPGroupHeader> listGH = new ArrayList<VPGroupHeader>();
				
		Cursor cur  = this.myGroupHeaderDL.getAllGroupHeader();
		
		int rowcount = cur.getCount();
		if(cur.moveToFirst()){
			for (int i=0;i<rowcount;i++)
			{	
				VPGroupHeader myGH = new VPGroupHeader();
				myGH.setVpghId(Integer.parseInt(cur.getString(cur.getColumnIndexOrThrow(VPGroupHeader_DL.getDbVpghId()))));
				myGH.setVpghCode(cur.getString(cur.getColumnIndexOrThrow(VPGroupHeader_DL.getDbVpghCode())));
				myGH.setVpghName(cur.getString(cur.getColumnIndexOrThrow(VPGroupHeader_DL.getDbVpghName())));
				myGH.setVpghDescription(cur.getString(cur.getColumnIndexOrThrow(VPGroupHeader_DL.getDbVpghDescription())));
				myGH.setVpghEnableFlag(cur.getString(cur.getColumnIndexOrThrow(VPGroupHeader_DL.getDbVpghEnableFlag())));
				listGH.add(myGH);
				cur.moveToNext();
			}
		}
		return listGH;
	}
	public Cursor getAllGroupHeaderCursor(){
		Cursor cur  =  this.myGroupHeaderDL.getAllGroupHeader();
		return cur;
	}
	public void addGroupHeader(VPGroupHeader myGH){
		this.myGroupHeaderDL.AddGroupHeader(myGH);
	}
	public void updateGroupHeader(VPGroupHeader myGH){
		this.myGroupHeaderDL.updateGroupHeader(myGH);
	}
	public void deleteGroupHeader(VPGroupHeader myGH){
		this.myGroupHeaderDL.deleteGroupHeader(myGH);
	}
	public void deleteGroupHeaderbyID(int _Vpgh_Id){
		this.myGroupHeaderDL.deleteGroupHeaderbyID(_Vpgh_Id);
	}

	
	@SuppressLint("DefaultLocale")
	public List<VPGroupHeader> getGroupHeaderInContains(String _contains, List<VPGroupHeader> _listGH) throws IllegalArgumentException, ParseException
	{
		
		if(_contains =="" ||_contains ==null )
		{
			return _listGH;
		}
		else
		{
			List<VPGroupHeader> listGH = new ArrayList<VPGroupHeader>();
			
			int rowcount = _listGH.size();
			for (int i=0;i<rowcount;i++)
				{	
					String _str = _listGH.get(i).getVpghName() ;
					if (_str.toLowerCase().contains(_contains.toLowerCase()))
					{
						VPGroupHeader myGH = new VPGroupHeader();
						myGH = _listGH.get(i);
						listGH.add(myGH);
						
					}
			
				}
			return listGH;
		}
		
	}
}
